//
//  SubjectViewController.m
//  MyLittleBrother
//
//  Created by speeda on 17/7/30.
//  Copyright © 2017年 speeda. All rights reserved.
//

#import "TalkViewController.h"
#import "SubjectViewController.h"
#import "TalkModel.h"

@interface TalkViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *talkTableView;
@property (nonatomic, strong) NSMutableArray *talkArray;

@property (nonatomic, assign) NSInteger page;

@end

@implementation TalkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTableView];
    
    [self AFNNewData];

}

- (void)initTableView {

    self.talkTableView.dataSource = self;
    self.talkTableView.delegate = self;
    self.talkTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.talkTableView.tableFooterView = [[UIView alloc] init];
    
//    __weak typeof(self) weakSelf = self;
    //默认block方法：设置下拉刷新
//    self.talkTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [weakSelf AFNNewData];
//    }];
//    [self.talkTableView.mj_header beginRefreshing];
    
    //默认block方法：设置上拉加载更多
//    self.talkTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        [weakSelf AFNMoreData];
//    }];
}



#pragma mark -- 获取话题
- (void)AFNNewData {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"command"] = @"gettopic";
    
    [AFHTTPSTool POSTRequestRequestWebsite:Url AndDictionary:dict success:^(NSURLSessionDataTask *dataTask, id responseObject) {
        
        [self.talkTableView.mj_header endRefreshing];
        
        NSDictionary *dic = responseObject;
        NSString *state = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([state isEqualToString:@"000000"]) {
            
            NSLog(@"获取话题 = %@",responseObject);
            [self.talkArray removeAllObjects];
    
            NSArray *topicArray = [TalkModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
            
            [self.talkArray addObjectsFromArray:topicArray];
            
            [self.talkTableView reloadData];
            
        } else {
            
            NSString *errorText = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            [SVProgressHUD showInfoWithStatus:errorText];
        }
        
        
    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
        
        [self.talkTableView.mj_header endRefreshing];
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}
//
//- (void)AFNMoreData {
//    
//    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//    dict[@"command"] = @"gettopic";
//    
//    [AFHTTPSTool POSTRequestRequestWebsite:Url AndDictionary:dict success:^(NSURLSessionDataTask *dataTask, id responseObject) {
//        
//        [self.talkTableView.mj_footer endRefreshing];
//        
//        NSDictionary *dic = responseObject;
//        NSString *state = [NSString stringWithFormat:@"%@",dic[@"code"]];
//        if ([state isEqualToString:@"000000"]) {
//            
//            NSLog(@"获取话题 = %@",responseObject);
//            
//            NSArray *topicArray = [TalkModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
//            NSString *pageString = [dic[@"data"] objectForKey:@"page"];
//            _page = pageString.integerValue;
//            _page++;
//            
//            [self.talkArray addObjectsFromArray:topicArray];
//            
//            [self.talkTableView reloadData];
//            
//        } else {
//            
//            NSString *errorText = [NSString stringWithFormat:@"%@",dic[@"msg"]];
//            [SVProgressHUD showInfoWithStatus:errorText];
//        }
//        
//        
//    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
//        
//        [self.talkTableView.mj_footer endRefreshing];
//        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
//    }];
//}



#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.talkArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *personalCell = @"personalCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:personalCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:personalCell];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    TalkModel *talkItem = self.talkArray[indexPath.row];
    cell.textLabel.text = talkItem.b_title;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TalkModel *talkItem = self.talkArray[indexPath.row];
    SubjectViewController *subjectVc = [[SubjectViewController alloc] init];
    subjectVc.topicid = talkItem.b_id;
    subjectVc.titleStr = talkItem.b_title;
    [self.navigationController pushViewController:subjectVc animated:YES];
}

- (NSMutableArray *)talkArray {
    
    if (!_talkArray) {
        _talkArray = [NSMutableArray array];
    }
    
    return _talkArray;
}

@end
